<?php

class Orders extends Eloquent {
    protected $table = 'orders';
    
    public static function Select($shop_id)
    {        
        try{
            return DB::table('orders')
            ->join('order_detail', 'orders.id', '=', 'order_detail.order_id')
            ->join('users', 'orders.user_id', '=', 'users.id')
            ->join('goods', 'order_detail.good_id', '=', 'goods.id')
            ->select('orders.*', 'order_detail.*','goods.name')
            ->where('users.shop_id' ,'=', $shop_id);
     
           // return DB::select($query_select);

        }
        catch(Exception $e)
        {
            throw $e;
        }

    }
    

    public static function Insert($input)
    {

        try{
            $users = Users::where('shop_id',$input['shop_id'])->get()->first();
            //if exists shop_id
            if($users){
                $order= new Orders();
                $order->order_number = $input['order_number'];
                $order->warehourse_id =$input['warehouse_id'];
                $order->total_money=$input['total_money'];
                $order->user_id=$input['user_id'];
                $order->status=$input['status'];
                $order->save();
                $order_id = $order->id;
               
                //if exists order id
                if($order_id){
                    $order_detail = new Order_Detail();
                    $order_detail ->good_id = $input['good_id'];
                    $order_detail ->number = $input['number'];
                    $order_detail ->import_price = $input['import_price'];
                    $order_detail ->tax = $input['tax'];
                    $order_detail ->discount = $input['discount'];
                    $order_detail ->comments = $input['comments'];
                    $order_detail ->order_id = $order_id;
                    $order_detail ->save();

                    return $order_id;
                }else{
                    return json_encode(array('error'=>'insert fail'));
                }
            }else{
                return json_encode(array('error'=>'insert fail d'));
            }    
            
            
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    
    
    public static function Edit($order_number, $warehouse_id, $total_money, $user_id, $status,$order_id,$good_id,$number,$import_price,$tax,$discount ,$comments)
    {

        try{

            $order= Orders::where('id',$order_id)->get()->first();
            $order->order_number = $order_number;
            $order->warehourse_id =$warehouse_id;
            $order->total_money=$total_money;
            $order->user_id=$user_id;
            $order->status=$status;
            $order->save();
            $order_id = $order->id;
            $orderdetail= Order_Detail::where('order_id',$order_id)->get()->first();
            if($order_id)
            {
               Order_Detail::where('order_id',$order_id)->update(array(
                                                                    'order_id'=>$order_id,
                                                                    'good_id'=>$good_id,
                                                                    'number'=>$number,
                                                                    'import_price'=>$import_price,
                                                                    'tax'=>$tax,
                                                                    'discount'=>$discount,
                                                                    'comments'=>$comments
                                                                )); 
                return json_encode(array('status'=>'success'));
            }else{
                $orderdetail= new Bill_detail();
                $orderdetail->order_id = $order_id;
                $orderdetail->good_id =$good_id;
                $orderdetail->number=$number;
                $orderdetail->import_price=$import_price;
                $orderdetail->tax=$tax;
                $orderdetail->discount=$discount;
                $orderdetail->total_money=$total_money;
                $orderdetail->comments=$comments;
                $orderdetail->save();
            }    
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    public static function Del($order_id){
        try{
            $query_delete ="DELETE FROM orders WHERE id = '$order_id'";
            $delete=DB::delete($query_delete);
            $query_delete2 ="DELETE FROM order_detail WHERE order_id = '$order_id'";
            $delete2 =DB::delete($query_delete2);
            
            return true;
        }
        catch(Exception $e){
            throw $e;
        }
    }
    public static function UpStatus($order_id,$status) 
        {
        try{
            $query_edit =" UPDATE orders SET status='$status' WHERE id = '$order_id' ";
            $query= DB::update($query_edit);
            return true;
         }
        catch(Exception $e)
        {
            throw $e;
        }
    }
   
}
